﻿
using System;
using System.Collections.Generic;
using NetBay.Core.BusinessEntities.ActiveDirectory;
using NetBay.Core.BusinessEntities.ActiveDirectory.Objects;
using System.Data;
using NetBay.Core.Log;

namespace Rio.Manager.Business.Repository.Translator
{
    public class ActiveDirectoryTranslator
    {
        #region Interface Setting

        #region Get

        /// <summary>
        /// Translates all the Rio and Ad correspondence.
        /// </summary>
        /// <param name="p_oIDataReader">The I data reader.</param>
        /// <returns></returns>
        public static List<AdInformationUserRio> TranslateAllRioADCorrespondence(IDataReader p_oIDataReader)
        {
            //Variable initialization.
            List<AdInformationUserRio> l_oReturnLstRioADCorrespondence = new List<AdInformationUserRio>();

            //Log.
            LogManager.Instance().LogDebug("ActiveDirectoryTranslator.TranslateAllRioADCorrespondence (p_oIDataReader <" + p_oIDataReader + ">)...");

            try
            {
                using (p_oIDataReader)
                {
                    // Read all rows
                    while (p_oIDataReader.Read())
                    {
                        l_oReturnLstRioADCorrespondence.Add(TranslateOneRioADCorrespondence(p_oIDataReader));
                    }
                }
            }
            catch (Exception ex)
            {
                //Error Log.
                LogManager.Instance().LogError("ActiveDirectoryTranslator.TranslateAllRioADCorrespondence Exception [" + ex.Message + "]");
            }

            //Return value.
            return l_oReturnLstRioADCorrespondence;
        }

        /// <summary>
        /// Translates only one Rio and AD Correspondence.
        /// </summary>
        /// <param name="p_oIDataReader">The I data reader.</param>
        private static AdInformationUserRio TranslateOneRioADCorrespondence(IDataReader p_oIDataReader)
        {
            //Variables initialization.
            AdInformationUserRio l_oReturnOneRioADCorrespondence = new AdInformationUserRio();

            //Log.
            LogManager.Instance().LogDebug("ActiveDirectoryTranslator.TranslateOneRioADCorrespondence ..");

            try
            {
                l_oReturnOneRioADCorrespondence.Type = (UserRioInformationType)Enum.Parse(typeof(UserRioInformationType), p_oIDataReader["t_ref_ad_usr_fieldmap_id"].ToString());
                l_oReturnOneRioADCorrespondence.Value = p_oIDataReader["t_ref_ad_usr_fieldmap_ad_name"].ToString();
                l_oReturnOneRioADCorrespondence.AdName = p_oIDataReader["t_ref_ad_usr_fieldmap_ad_conf"].ToString();
            }
            catch (Exception ex)
            {
                //Error log.
                LogManager.Instance().LogError("ActiveDirectoryTranslator.TranslateOneRioADCorrespondence Exception [" + ex.Message + "]");

                //Return null.
                l_oReturnOneRioADCorrespondence = null;
            }

            //Return value.
            return l_oReturnOneRioADCorrespondence;
        }
        
        /// <summary>Retrieves mapped object</summary>
        /// <param name="p_oIDataReader"></param>
        /// <param name="p_lGroupId"></param>
        /// <returns></returns>
        public static List<AdObject> GetAdMappedObject(IDataReader p_oIDataReader, long p_lGroupId)
        {
            //Variable initialization.
            var l_oReturnLstAdobject = new List<AdObject>();

            //Log.
            LogManager.Instance().LogDebug("ActiveDirectoryTranslator.GetAllAdMappedObject ...");

            try
            {
                using (p_oIDataReader)
                {
                    // Read all rows
                    while (p_oIDataReader.Read())
                    {

                        if (int.Parse(p_oIDataReader["OBJECT_TYPE"].ToString()) == (int)AdObjectType.Group)
                        {
                            l_oReturnLstAdobject.Add(TranslateOneAdGroup(p_oIDataReader, p_lGroupId));
                        }
                        else if (int.Parse(p_oIDataReader["OBJECT_TYPE"].ToString()) == (int)AdObjectType.User)
                        {
                            l_oReturnLstAdobject.Add(TranslateOneAdUser(p_oIDataReader, p_lGroupId));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //Error Log.
                LogManager.Instance().LogError("ActiveDirectoryTranslator.GetAllAdMappedObject Exception [" + ex.Message + "]");
            }

            //Return value.
            return l_oReturnLstAdobject;
        }

        /// <summary>Translate on user.</summary>
        /// <param name="p_oIDataReader"></param>
        /// <param name="p_lGroupId"></param>
        /// <returns></returns>
        private static AdUser TranslateOneAdUser(IDataReader p_oIDataReader, long p_lGroupId)
        {
            //Variables initialization.
            var l_oReturnOneAdUser = new AdUser();

            //Log.
            LogManager.Instance().LogDebug("ActiveDirectoryTranslator.TranslateOneAdUser ...");

            try
            {
                l_oReturnOneAdUser.FirstName = p_oIDataReader["ruser_prenom"].ToString();//"(UserRioInformationType)System.Enum.Parse(typeof(UserRioInformationType), p_oIDataReader["t_ref_ad_usr_fieldmap_id"].ToString());
                l_oReturnOneAdUser.Name = p_oIDataReader["ruser_nom"].ToString();// = p_oIDataReader["t_ref_ad_usr_fieldmap_ad_name"].ToString();
                l_oReturnOneAdUser.Email = p_oIDataReader["ruser_email"].ToString();
                l_oReturnOneAdUser.GuidOfAdObject = Guid.Parse(p_oIDataReader["r_adsync_map_ad_object_guid"].ToString());
                l_oReturnOneAdUser.Login = p_oIDataReader["ruser_login"].ToString();
                l_oReturnOneAdUser.TypeOfAdObject = AdObjectType.User;
                l_oReturnOneAdUser.DisplayName = l_oReturnOneAdUser.InformationName;
                l_oReturnOneAdUser.MappedGroupIdentifier = p_lGroupId;
                l_oReturnOneAdUser.AdName = p_oIDataReader["r_adsync_map_ad_config"].ToString();
            }
            catch (Exception ex)
            {
                //Error log.
                LogManager.Instance().LogError("ActiveDirectoryTranslator.TranslateOneAdUser Exception [" + ex.Message + "]");

                //Return null.
                l_oReturnOneAdUser = null;
            }

            //Return value.
            return l_oReturnOneAdUser;
        }

        /// <summary>Translate on group.</summary>
        /// <param name="p_oIDataReader"></param>
        /// <param name="p_lGroupId"></param>
        /// <returns></returns>
        private static AdGroup TranslateOneAdGroup(IDataReader p_oIDataReader, long p_lGroupId)
        {
            //Variables initialization.
            var l_oReturnOneAdGroup = new AdGroup();

            //Log.
            LogManager.Instance().LogDebug("ActiveDirectoryTranslator.TranslateOneAdGroup ...");

            try
            {
                l_oReturnOneAdGroup.Name = p_oIDataReader["r_adsync_map_ad_object_name"].ToString();
                l_oReturnOneAdGroup.GuidOfAdObject = Guid.Parse(p_oIDataReader["r_adsync_map_ad_object_guid"].ToString());
                l_oReturnOneAdGroup.TypeOfAdObject = AdObjectType.Group;
                l_oReturnOneAdGroup.DisplayName = l_oReturnOneAdGroup.InformationName;
                l_oReturnOneAdGroup.MappedGroupIdentifier = p_lGroupId;
                l_oReturnOneAdGroup.AdName = p_oIDataReader["r_adsync_map_ad_config"].ToString();
            }
            catch (Exception ex)
            {
                //Error log.
                LogManager.Instance().LogError("ActiveDirectoryTranslator.TranslateOneAdGroup Exception [" + ex.Message + "]");

                //Return null.
                l_oReturnOneAdGroup = null;
            }

            //Return value.
            return l_oReturnOneAdGroup;
        }

        /// <summary>
        /// Translates Ad Name Mapped.
        /// </summary>
        /// <param name="p_oIDataReader">The I data reader.</param>
        /// <returns></returns>
        public static string TranslateAdNameMapped(IDataReader p_oIDataReader)
        {
            //Variable initialization.
            string l_sReturnAdNameMapped = string.Empty;

            //Log.
            LogManager.Instance().LogDebug("ActiveDirectoryTranslator.TranslateAdNameMapped (p_oIDataReader <" + p_oIDataReader + ">)...");

            try
            {
                using (p_oIDataReader)
                {
                    p_oIDataReader.Read();
                    l_sReturnAdNameMapped = p_oIDataReader["t_ref_ad_usr_fieldmap_ad_conf"].ToString();
                }
            }
            catch (Exception ex)
            {
                //Error Log.
                LogManager.Instance().LogError("ActiveDirectoryTranslator.TranslateAdNameMapped Exception [" + ex.Message + "]");
            }

            //Return value.
            return l_sReturnAdNameMapped;
        }

        #endregion Get

        #endregion Interface Setting
    }
}